<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.rapid-framework.org.cn/rapid" prefix="rapid"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<rapid:override name="frame-header-style">
	<style type="text/css">
        #updateTag {
	    width: 100px;
	    height: 30px;
	    line-height: 30px;
	    background: #009F95;
	    color: #FFFFFF;
	  text-align: center;
         }
         /*覆盖 layui*/
.layui-input-block {
	margin: 0px 10px;
}

.layui-table {
	margin-top: 0;
}

.layui-col-md4 {
	padding: 10px;
}

.layui-col-md8 {
	padding: 10px;
}

.layui-btn {
	margin: 2px 0 !important;
}
  </style>

</rapid:override>
<rapid:override name="frame-header-script">
 <script type="text/javascript">
		function TgNamefun() {
			var tagName = $("#TgName").val();
			$
					.ajax({
						type : "GET",
						url : "tag/tag-Name",
						async : true,
						contentType : "application/json",
						data : {
							tagName : tagName
						},
						success : function(data) {
							if (data == "error") {
								$("#sp")
										.html(
												"<span><font color='red'>不可用</font></span><br>");
								$("#buttonId").attr('disabled', true);
							} else {
								if (tagName == '') {
									$("#sp")
											.html(
													"<span><font color='red'>不可用</font></span><br>");
									$("#buttonId").attr('disabled', true);
								} else {
									$("#sp")
											.html(
													"<span><font color='green'>可用</font></span><br>");
									$("#buttonId").attr('disabled', false);
								}
							}
						}
					});

		}
		function editfun(tagId) {
			  $.ajax({
					type : "GET",
					url : "tag/updateAjax",
					async : true,
					contentType : "application/json",
					data : {tagId : tagId},
					success : function(data) {
						window.tag = eval("("+data+")");
						$('#updateTag').click();
						
					}
				});
		}
		console.log(tag);
	</script>

</rapid:override>
<div id="updateTag" style="display: none;"></div>
<rapid:override name="frame-content">
	<blockquote class="layui-elem-quote">
		<span class="layui-breadcrumb" lay-separator="/"> <a
			href="/admin">首页</a> <a><cite>标签列表</cite></a>
		</span>
	</blockquote>
	<div class="layui-row">
		<div class="layui-col-md4">
			<form class="layui-form" method="post" id="myForm"
				action="tag/insertSubmit">
				<div class="layui-form-item">
					<div class="layui-input-block">
						<strong>添加标签</strong>
					</div>
					<div class="layui-input-block">
						名称 <span style="color: #FF5722;">*</span> <input type="text" id="TgName" onblur="TgNamefun()"
							name="tagName" placeholder="请输入标签名称" autocomplete="off"
							class="layui-input" required><span id="sp"></span><br>
					</div>
					<br>
					<div class="layui-input-block">
						标签描述 <input type="text" name="tagDescription"
							placeholder="请输入标签描述" autocomplete="off" class="layui-input">
					</div>
					<br>
					<div class="layui-input-block">
						<button class="layui-btn" lay-filter="formDemo" type="submit" id="buttonId">添加</button>
					</div>
				</div>
			</form>
			<blockquote class="layui-elem-quote layui-quote-nm">
				温馨提示：
				<ul>
					<li>1、标签名必选，建议不要太长</li>
					<li>2、标签名勿重复</li>
				</ul>
			</blockquote>
		</div>
		<div class="layui-col-md8">

			<table class="layui-table">
				<colgroup>
					<col width="300">
					<col width="50">
					<col width="100">
					<col width="50">
				</colgroup>
				<thead>
					<tr>
						<th>名称</th>
						<th>文章数</th>
						<th>操作</th>
						<th>ID</th>
					</tr>
				</thead>
				<tbody>
					<c:forEach items="${tags }" var="tag">
						<tr>
							<td><a href="/tag/${tag.tagId }" target="_blank">${tag.tagName }</a></td>
							<td><a href="/tag/${tag.tagId }" target="_blank"
								lay-data="{sort:true}">${tag.articleCount }</a></td>
							<td><c:choose>

									<c:when test="${tag.articleCount == 0 }">
										<a class="layui-btn layui-btn-mini" onclick="editfun(${tag.tagId})">编辑</a>
										<a href="tag/delete/${tag.tagId }"
											class="layui-btn layui-btn-danger layui-btn-mini"
											onclick="return confirmDelete()">删除</a>
									</c:when>
									<c:otherwise>
										<a class="layui-btn layui-btn-mini" onclick="editfun(${tag.tagId})">编辑</a>
									</c:otherwise>
								</c:choose></td>
							<td>${tag.tagId }</td>
						</tr>

					</c:forEach>

				</tbody>
			</table>
			<blockquote class="layui-elem-quote layui-quote-nm">
				温馨提示：
				<ul>
					<li>如果该标签包含文章，将不可删除</li>
				</ul>
			</blockquote>
		</div>
	</div>

</rapid:override>
<rapid:override name="frame-footer-script">
    <script type="text/javascript">
$("#updateTag").on("click", function() {
    layer.open({
        type : 2,
        title : '编辑',
        area : [ '500px', '500px' ],
        fix : false, //
        content : 'jsp/Tag/tag-update.jsp',
        end : function() {
            dataTable.reloadTable();
        }
    });
});
    </script>
</rapid:override>
<%@ include file="../framework.jsp"%>